package com.wutuo.tag.provider.dao;

import com.wutuo.tag.api.request.TagRequest;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class TagSqlProvider extends BaseTagSqlProvider {
    public String list(TagRequest request) {
        BEGIN();
        getSelected();
        FROM("tag a");
        if (request.getCmInstitutionId() != null) {
            SqlBuilder.WHERE(" a.cm_institution_id = #{cmInstitutionId,jdbcType=VARCHAR}");
        }
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public String listByUserId(TagRequest request) {
        BEGIN();
        getSelected();
        FROM("tag a ,tag_relations b ");
        SqlBuilder.WHERE("  a.id = b.tag_id");
        if (request.getUserId() != null) {
            SqlBuilder.WHERE(" b.user_id = #{userId,jdbcType=VARCHAR}");
        }

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        SqlBuilder.WHERE(" b.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {
        SELECT(
            " a.id as id, a.tag_name as tagName, a.tag_type as tagType, a.tag_describe as tagDescribe,a.cm_institution_id as cmInstitutionId");
        this.getCommonSelected();
    }

    /**
     * 获取公共字段
     */
    public static void getCommonSelected() {
        SELECT(
            " a.create_by as createBy, a.create_date as createDate, a.update_by as updateBy, a.update_date, a.remarks, a.del_flag as delFlag");
    }
}